﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyHealthByDesign.Domain;

namespace MyHealthByDesign.Views
{
    /// <summary>
    /// Description:
    /// Displays health history form and data
    /// 
    /// Author: Sanjeev Shrestha
    /// Date Created: 10/21/2011
    /// 
    /// Other code files referenced:
    /// None.
    /// 
    /// Regression Test Information:
    /// None.
    /// 
    /// Revision History:
    /// 10/27/2011 Sanjeev Shrestha - Added logic for login check.
    /// 10/28/2011 Mark Servais - Added question logic.
    /// 10/30/2011 Mark Servais - Code Cleanup.
    /// 11/03/2011 Mark Servais - Added Master Page control access fix.
    /// 
    /// Known Faults:
    /// None.
    /// </summary>
    public partial class MyHealthHistory : System.Web.UI.Page
    {
        Domain.Domain domain = new Domain.Domain();
        private const int CATEGORYHEALTHHISTORY = 3;

        /// <summary>
        /// Page Init event handler - load question panels
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Controls loaded for programatic access
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Init(object sender, EventArgs e)
        {
            // retrieve questions for page
            List<Question> lstQuestions = domain.RetrieveQuestions(CATEGORYHEALTHHISTORY);

            // loop through questions to create display panels
            foreach (Question q in lstQuestions)
            {
                phPanels.Controls.Add(q.DisplayQuestion());
            }

            // store questions in session in order to retrieve answers on post back
            Session["Questions"] = lstQuestions;

            MasterPage ctl00 = FindControl("ctl00") as MasterPage;
            ContentPlaceHolder MainContent = ctl00.FindControl("MainContent") as ContentPlaceHolder;
            domain.SetInitialQuestionAnswers(MainContent);
        }

        /// <summary>
        /// Page Load event handler - check if user is logged in
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Page displayed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!domain.IsUserLoggedIn())
            {
                Response.Redirect("../Default.aspx");
            }
        }

        /// <summary>
        /// Update button event handler - save question answers
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// redirect to Health risk factors
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnContinue_Click(object sender, EventArgs e)
        {
            // Go through question fields and retrieve values
            if (Session["Questions"] != null)
            {
                MasterPage ctl00 = FindControl("ctl00") as MasterPage;
                ContentPlaceHolder MainContent = ctl00.FindControl("MainContent") as ContentPlaceHolder;

                domain.SaveQuestionAnswers(MainContent); // retrieves values and save to database

                //where am I going/
                Response.Redirect("MyHealthRiskFactors.aspx");
            }
        }
    }
}